package cn.cloudreal.cbms.fault.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import cn.cloudreal.cbms.common.util.Constants;
import cn.cloudreal.cbms.common.util.DatadicContext;
import cn.cloudreal.cbms.common.util.PageBean;
import cn.cloudreal.cbms.fault.service.FaultService;
import cn.cloudreal.cbms.vo.Dictionary;
import cn.cloudreal.cbms.vo.Equip;
import cn.cloudreal.cbms.vo.Fault;

import com.opensymphony.xwork2.ActionSupport;

public class FaultAction extends ActionSupport {

	/** serialVersionUID */
	private static final long serialVersionUID = 1L;

	/** 日志工具类实例 */
	private static final Logger log = Logger.getLogger(FaultAction.class);

	/** 服务 */
	private FaultService faultService;

	/** 当前页 */
	int currentPage;

	/** 分页对象 */
	PageBean page;

	private int id;
	private String date;
	private String responseTime;
	private String arriveTime;
	private String repairTime;
	private String lineId;
	private String workOrder;
	private String deviceType;
	private int deviceId;
	private String faultBlock;
	private String faultProblem;
	private String solutionMethod;
	private String faultReason;
	private String faultLocation;
	private String repairMan;
	private String repairTeam;
	private String problemTrack;
	private String repairMethod;
	private String replaceInfo;
	private String replacePrice;
	private String comments;
	private String factoryMan;

	private Fault fault;

	private List<Fault> faults = new ArrayList<Fault>();

	/** 字典 */
	List<Dictionary> repairMethodSelect = new ArrayList<Dictionary>();
	List<Dictionary> deviceTypeSelect = new ArrayList<Dictionary>();

	/**
	 * 列表
	 * 
	 * @return
	 */
	public String list() {
		// 打印进入方法体日志
		if (log.isInfoEnabled()) {
			log.info("Enter FaultAction list() method");
		}
		// 条件
		Map<Object, Object> condition = new HashMap<Object, Object>();
		condition.put("date", date);
		condition.put("workOrder", workOrder);
		// 分页对象初始化
		page = new PageBean(currentPage == 0 ? 1 : currentPage,
				Constants.PAGE_SIZE);
		// 查询总记录数
		int totalRecords = faultService.getFaultListCount(condition);
		//
		page.setTotalRecords(totalRecords);
		// 查询
		faults = faultService.getFaultList(condition, page);
		//
		for (Fault f : faults) {
			//
			f.setLineIdText(f.getLineId());
			//
			Dictionary dicRepairMethod = DatadicContext.getInstance()
			.getDatadicObject(null, f.getRepairMethod());
			if(dicRepairMethod != null){
				f
				.setRepairMethodText(dicRepairMethod.getDatavalue());
			}
			//
			Dictionary dicDeviceTypeText = DatadicContext.getInstance()
			.getDatadicObject(null, f.getDeviceType());
			if(dicDeviceTypeText != null){
				f.setDeviceTypeText(dicDeviceTypeText.getDatavalue());
			}
		}
		// 打印退出方法体日志
		if (log.isInfoEnabled()) {
			log.info("Exit FaultAction list() method");
		}
		return SUCCESS;
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	public String delete() {
		// 打印进入方法体日志
		if (log.isInfoEnabled()) {
			log.info("Enter FaultAction delete() method");
		}
		// 删除
		faultService.deleteFault(fault);
		// 打印退出方法体日志
		if (log.isInfoEnabled()) {
			log.info("Exit FaultAction delete() method");
		}
		return SUCCESS;
	}

	/**
	 * 修改
	 * 
	 * @return
	 */
	public String initModify() {
		// 打印进入方法体日志
		if (log.isInfoEnabled()) {
			log.info("Enter FaultAction initModify() method");
		}
		// 查询
		fault = faultService.detailFault(fault);
		//
		repairMethodSelect = DatadicContext.getInstance().getDicDataListByCode(
				"GZWXFS");
		deviceTypeSelect = DatadicContext.getInstance().getDicDataListByCode(
				"SBLX");
		// 打印退出方法体日志
		if (log.isInfoEnabled()) {
			log.info("Exit FaultAction initModify() method");
		}
		return SUCCESS;
	}

	/**
	 * 修改
	 * 
	 * @return
	 */
	public String modify() {
		// 打印进入方法体日志
		if (log.isInfoEnabled()) {
			log.info("Enter FaultAction modify() method");
		}
		//
		Fault modifyFault = new Fault(id, date, responseTime, arriveTime,
				repairTime, lineId, workOrder, deviceType, deviceId,
				faultBlock, faultProblem, solutionMethod, faultReason,
				faultLocation, repairMan, repairTeam, problemTrack,
				repairMethod, replaceInfo, replacePrice, comments, factoryMan);
		// 编辑
		faultService.modifyFault(modifyFault);
		// 打印退出方法体日志
		if (log.isInfoEnabled()) {
			log.info("Exit FaultAction modify() method");
		}
		return SUCCESS;
	}

	/**
	 * 详情
	 * 
	 * @return
	 */
	public String detail() {
		// 打印进入方法体日志
		if (log.isInfoEnabled()) {
			log.info("Enter FaultAction detail() method");
		}
		// 查询
		fault = faultService.detailFault(fault);
		// 打印退出方法体日志
		if (log.isInfoEnabled()) {
			log.info("Exit FaultAction detail() method");
		}
		return SUCCESS;
	}

	/**
	 * 新增init
	 * 
	 * @return
	 */
	public String initAdd() {
		// 打印进入方法体日志
		if (log.isInfoEnabled()) {
			log.info("Enter FaultAction initAdd() method");
		}
		//
		repairMethodSelect = DatadicContext.getInstance().getDicDataListByCode(
				"GZWXFS");
		deviceTypeSelect = DatadicContext.getInstance().getDicDataListByCode(
				"SBLX");
		// 打印退出方法体日志
		if (log.isInfoEnabled()) {
			log.info("Exit FaultAction initAdd() method");
		}
		return SUCCESS;
	}

	/**
	 * 新增
	 * 
	 * @return
	 */
	public String add() {
		// 打印进入方法体日志
		if (log.isInfoEnabled()) {
			log.info("Enter FaultAction add() method");
		}
		//
		Fault addFault = new Fault(id, date, responseTime, arriveTime,
				repairTime, lineId, workOrder, deviceType, deviceId,
				faultBlock, faultProblem, solutionMethod, faultReason,
				faultLocation, repairMan, repairTeam, problemTrack,
				repairMethod, replaceInfo, replacePrice, comments, factoryMan);
		// 编辑
		faultService.addFault(addFault);
		// 打印退出方法体日志
		if (log.isInfoEnabled()) {
			log.info("Exit FaultAction add() method");
		}
		return SUCCESS;
	}

	public PageBean getPage() {
		return page;
	}

	public void setPage(PageBean page) {
		this.page = page;
	}

	public void setFaultService(FaultService faultService) {
		this.faultService = faultService;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getResponseTime() {
		return responseTime;
	}

	public void setResponseTime(String responseTime) {
		this.responseTime = responseTime;
	}

	public String getArriveTime() {
		return arriveTime;
	}

	public void setArriveTime(String arriveTime) {
		this.arriveTime = arriveTime;
	}

	public String getRepairTime() {
		return repairTime;
	}

	public void setRepairTime(String repairTime) {
		this.repairTime = repairTime;
	}

	public String getLineId() {
		return lineId;
	}

	public void setLineId(String lineId) {
		this.lineId = lineId;
	}

	public String getWorkOrder() {
		return workOrder;
	}

	public void setWorkOrder(String workOrder) {
		this.workOrder = workOrder;
	}

	public String getDeviceType() {
		return deviceType;
	}

	public void setDeviceType(String deviceType) {
		this.deviceType = deviceType;
	}

	public int getDeviceId() {
		return deviceId;
	}

	public void setDeviceId(int deviceId) {
		this.deviceId = deviceId;
	}

	public String getFaultBlock() {
		return faultBlock;
	}

	public void setFaultBlock(String faultBlock) {
		this.faultBlock = faultBlock;
	}

	public String getFaultProblem() {
		return faultProblem;
	}

	public void setFaultProblem(String faultProblem) {
		this.faultProblem = faultProblem;
	}

	public String getSolutionMethod() {
		return solutionMethod;
	}

	public void setSolutionMethod(String solutionMethod) {
		this.solutionMethod = solutionMethod;
	}

	public String getFaultReason() {
		return faultReason;
	}

	public void setFaultReason(String faultReason) {
		this.faultReason = faultReason;
	}

	public String getFaultLocation() {
		return faultLocation;
	}

	public void setFaultLocation(String faultLocation) {
		this.faultLocation = faultLocation;
	}

	public String getRepairMan() {
		return repairMan;
	}

	public void setRepairMan(String repairMan) {
		this.repairMan = repairMan;
	}

	public String getRepairTeam() {
		return repairTeam;
	}

	public void setRepairTeam(String repairTeam) {
		this.repairTeam = repairTeam;
	}

	public String getProblemTrack() {
		return problemTrack;
	}

	public void setProblemTrack(String problemTrack) {
		this.problemTrack = problemTrack;
	}

	public String getRepairMethod() {
		return repairMethod;
	}

	public void setRepairMethod(String repairMethod) {
		this.repairMethod = repairMethod;
	}

	public String getReplaceInfo() {
		return replaceInfo;
	}

	public void setReplaceInfo(String replaceInfo) {
		this.replaceInfo = replaceInfo;
	}

	public String getReplacePrice() {
		return replacePrice;
	}

	public void setReplacePrice(String replacePrice) {
		this.replacePrice = replacePrice;
	}

	public String getComments() {
		return comments;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}

	public String getFactoryMan() {
		return factoryMan;
	}

	public void setFactoryMan(String factoryMan) {
		this.factoryMan = factoryMan;
	}

	public Fault getFault() {
		return fault;
	}

	public void setFault(Fault fault) {
		this.fault = fault;
	}

	public List<Fault> getFaults() {
		return faults;
	}

	public List<Dictionary> getRepairMethodSelect() {
		return repairMethodSelect;
	}

	public List<Dictionary> getDeviceTypeSelect() {
		return deviceTypeSelect;
	}

}
